<?php
namespace Home\Controller;
use Home\Model\WorkModel;
use Think\Controller;
class IndexController extends Controller {
    public $template = null;
    /**
     * 默认显示页面
     */
    public function index()
    {
        if ($this->checkLogin()) {
            $info = json_decode(session('info'), true);
            $this->assign('username', $info['name']);
            $this->assign('ctime', date('Y-m-d H:i:s', (int)$info['ctime']));
            $this->assign('logo', $info['img']);
            $this->assign('template', C('Admin_Template'));
            $this->display('index');
        } else {
            $this->display('login');
        }
    }

    /**
     * 检查用户是否登陆
     */
    protected function checkLogin()
    {
        if (session('uid')) {
            return true;
        }
        return false;
    }

    /**
     * 注册页面跳转
     */
    public function register()
    {
        $this->display('register');
    }

    /**
     * 注册
     */
    public function reg()
    {
        if ($this->checkAccount($_POST['account'])) {
            $this->error('该账号已被注册', 'index.php?s=Home/Index//register');
            return false;
        }
        $data = array(
            'account' => trim($_POST['account']),
            'password' => md5($_POST['password']),
            'name' => trim($_POST['name']),
            'phone' => trim($_POST['phone']),
            'ctime' => time(),
            'rtime' => time(),
            'role' => 2
        );
        $result = M('user')->add($data);
        if ($result) {
            $this->success('注册成功','index.php?s=Home/Index//index');
            return true;
        }
        $this->error('注册失败', 'index.php?s=Home/Index//register');
        return false;
    }

    /**
     * 检测账户是否存在
     * @param $account
     * @return bool
     */
    protected function checkAccount($account)
    {
        $where = array(
            'account' => trim($account)
        );
        $result = M('user')->where($where)->find();
        if ($result) {
            return true;
        }
        return false;
    }

    /*
     * 登陆
     */
    public function login()
    {
        $account = trim($_POST['account']);
        $password = trim($_POST['password']);
        $where = array(
            'account' => $account,
            'password' => md5($password)
        );
        $result = M('user')->where($where)->find();
        if ($result) {
            session('uid', $result['id']);
            $info = json_encode($result);
            session('info', $info);
            $data = [
                'uid' => $result['id'],
                'name' => $result['name'],
                'login_time' => time()
            ];
            M('log')->add($data);
            $this->success('登录成功', 'index.php?s=Home/Index/index');
            return true;
        }
        $this->error('登录失败', 'index.php?s=Home/Index/index');
        return false;
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        session_destroy();
        $this->success('退出登录成功','index.php?s=Home/Index/index');
    }

    /**
     * 个人信息
     */
    public function info()
    {
        $info = json_decode(session('info'), true);
        $result = M('log')->where(['uid' => session('uid')])->limit(10)->select();
        $this->assign('log', $result);
        $this->assign('user', $info);
        $this->display('info');
    }

    /**
     * 修改信息
     */
    public function saveInfo()
    {
        $data = array(
            'name' => trim($_POST['name']),
            'phone' => trim($_POST['phone'])
        );
        $result = M('user')->where(array('id' => session('uid')))->save($data);
        if ($result !== false) {
            $return = array(
                'code' => 0
            );
        } else {
            $return = array(
                'code' => -1
            );
        }
        $this->ajaxReturn($return, 'json');
    }
}